﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Employee_CreateInterview : System.Web.UI.Page
{
    Vacancy V = new Vacancy();
    protected void Page_Load(object sender, EventArgs e)
    {

        txtEmployeeCreate.Text = V.SelectNameEmployee("H0001");
        btnCreate.Enabled = false;
        AddItemsVacancy();
    }
    private void AddItemsVacancy()
    {

        if (ddltitlevacancy.Items.Count < 2)
        {
            DataTable tb = V.SelectVacancy("H0001");
            if (tb.Rows.Count > 0)
            {
                foreach (DataRow item in tb.Rows)
                {
                    ddltitlevacancy.Items.Add(item["Title"].ToString());
                }
            }
        }
    }
    private void AddItemsApplicant()
    {
        if (ddlApplicant.Items.Count < 2)
        {
            DataTable tb = V.SelectApplicant(V.ReturnVacancyID(ddltitlevacancy.SelectedValue));
            if (tb.Rows.Count > 0)
            {
                foreach (DataRow item in tb.Rows)
                {
                    ddlApplicant.Items.Add(item["FullName"].ToString() + "-" + item["ApplicantID"].ToString());
                }
            }
        }
    }
    private void AddItemsInterviewer()
    {
        if (ddlInterviewer.Items.Count < 2)
        {
            DataTable tb = V.SelectInterviewer(V.ReturnVacancyID(ddltitlevacancy.SelectedValue));
            if (tb.Rows.Count > 0)
            {
                foreach (DataRow item in tb.Rows)
                {
                    ddlInterviewer.Items.Add(item["FullName"].ToString() + "-" + item["InterViewerID"].ToString());
                }
            }
        }
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        if (ddltitlevacancy.SelectedValue.Equals("Please choise title vacancy"))
        {
            lblErrortitlevacancy.Text = "(*)";
            lblError.Text = "Please choise value for title vacancy.";
        }
        else
        {
            lblErrortitlevacancy.Text = "";
            if (txtDateInterview.Text.Equals(""))
            {
                lblErrorDateInterview.Text = "(*)";
                lblError.Text = "Date Interview can't be empty.";
            }

            else
            {
                lblErrorDateInterview.Text = "";
                if (ddlApplicant.SelectedValue.Equals("Please choise Applicant"))
                {
                    lblErrorApplicant.Text = "(*)";
                    lblError.Text = "Please choise value for Applicant Name.";
                }
                else
                {
                    lblErrorApplicant.Text = "";
                    if (ddlInterviewer.SelectedValue.Equals("Please choise Interviewer"))
                    {
                        lblErrorInterviewer.Text = "(*)";
                        lblError.Text = "Please choise value for Interviewer.";
                    }
                    else
                    {
                        lblErrorInterviewer.Text = "";
                        if (txtTimeStart.Text.Equals(""))
                        {
                            lblErrorTime.Text = "(*)";
                            lblError.Text = "Time Details can't be empty.";
                        }
                        else
                        {
                            lblErrorTime.Text = "";
                            lblError.Text = "";
                            try
                            {
                                string ApplicantID = ddlApplicant.SelectedValue.Substring(ddlApplicant.SelectedValue.LastIndexOf("-"), 5);
                                V.InsertintoInterviewdetail("A0001", "P0001", "Date", "Time", 2);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ddltitlevacancy.ClearSelection();
        ddlApplicant.ClearSelection();
        ddlInterviewer.ClearSelection();
        txtDateInterview.Text = "";
        txtTimeStart.Text = "";
        lblError.Text = "";
        lblErrorApplicant.Text = "";
        lblErrorDateInterview.Text = "";
        lblErrorInterviewer.Text = "";
        lblErrorTime.Text = "";
        lblErrortitlevacancy.Text = "";
        lblSuccess.Text = "";
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        lblErrorDateInterview.Text = "";
        lblError.Text = "";
        if (Calendar1.SelectedDate >= DateTime.Now)
        {
            txtDateInterview.Text = Calendar1.SelectedDate.ToShortDateString();
        }
        else
        {
            lblErrorDateInterview.Text = "(*)";
            lblError.Text = "Interview date is the date in the future.";
        }
    }
    protected void ddltitlevacancy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddltitlevacancy.SelectedIndex != 0)
        {
            for (int i = 1; i < ddlApplicant.Items.Count; i++)
            {
                ddlApplicant.Items.RemoveAt(i);
            }
            for (int i = 1; i < ddlInterviewer.Items.Count; i++)
            {
                ddlInterviewer.Items.RemoveAt(i);
            }
            AddItemsApplicant();
            AddItemsInterviewer();
        }
    }
}